<?php
/*------------------------------------------------------------------------
# plugin_ilike - XIPAT Like Article Component
# version: 1.0
# ------------------------------------------------------------------------
# author    Duong Tien Dung - www.xipat.com
# copyright Copyright (C) 2013 www.xipat.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.xipat.com
# Technical Support:  http://www.xipat.com/support.html
-------------------------------------------------------------------------*/
// no direct access
defined('_JEXEC') or die;
$lang = JFactory::getLanguage();
$lang->load('plg_ilike', JPATH_ADMINISTRATOR);
/**
 * Xplike plugin.
 *
 * @package		Joomla.Plugin
 * @subpackage	Content.vote
 */
class plgContentIlike extends JPlugin {

    /**
     * Constructor
     *
     * @access      protected
     * @param       object  $subject The object to observe
     * @param       array   $config  An array that holds the plugin configuration
     * @since       1.5
     */
    public function __construct(& $subject, $config) {
        parent::__construct($subject, $config);
        $this->loadLanguage();
    }
    /**
     * @since	1.6
     */
    function onContentAfterDisplay($context, &$article, &$params, $limitstart = 0) {
        //print_r($this->params); die;// print_r($article); 
        // Just work on View Article
        if (JRequest::getVar("view") != 'article')
            return false;
        //$article_layout=$params->get('article_layout');
        //print_r($params->get('article_layout'));
        if (strlen($params->get('article_layout')) < 1) {
            return false;
        }
        $input = JFactory::getApplication()->input;
        if (JFactory::getDocument()->getType() !== 'html' || $input->get('tmpl', '', 'cmd') === 'component') {
            return true;
        }

        $doc = & JFactory::getDocument();
        $user = JFactory::getUser();
        if ($this->params->get('jquery', 1)) {
            $doc->addScript('https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js');
        }
        $js = "var baseurl='" . JUri::base() . "'";
        $doc->addScriptDeclaration($js);
        $doc->addScript(JUri::base() . 'components/com_ilike/html/js/xplike.js');
        if($this->params->get('theme_boxes') == 1) $doc->addStyleSheet(JUri::base() . 'components/com_ilike/html/css/style1.css'); 
        elseif($this->params->get('theme_boxes') == 2) $doc->addStyleSheet(JUri::base() . 'components/com_ilike/html/css/style2.css'); 
        else $doc->addStyleSheet(JUri::base() . 'components/com_ilike/html/css/xplike.css'); 
        $option = JRequest::getVar('option');
        $id = JRequest::getVar('id');
        $db = & JFactory::getDbo();
        $sql = "SELECT likes,dislikes FROM #__xplike WHERE component='" . $option . "' AND content_id=" . $id;
        $db->setQuery($sql);
        $currentlike = $db->loadObject();
        $useraction = array();
        if ($currentlike AND $user->id) {
            $sql = "SELECT * FROM #__xplike_log WHERE component='" . $option . "' AND user_id=" . $user->id . " AND content_id=" . $id;
            $db->setQuery($sql);
            $useraction=$db->loadObject();
        }
        elseif($currentlike AND $user->id == 0) {
            $sql = "SELECT * FROM #__xplike_log WHERE component='" . $option . "' AND ipaddress='".$_SERVER['REMOTE_ADDR']."' AND content_id=" . $id;
            $db->setQuery($sql);
            $useraction=$db->loadObject();            
        }
		
		$sql="SELECT c.created_by,u.name FROM #__content c LEFT JOIN #__users u ON u.id=c.created_by  WHERE c.id = ".$id;
		$db->setQuery($sql);
		$content=$db->loadObject();

		$sql="SELECT COUNT(*) AS m FROM #__content WHERE state=1 AND created_by = ".$content->created_by;
		$db->setQuery($sql);
		$totalcontent= $db->loadObject()->m;     
        if ($this->params->get('require_login', 1)) {               				
        $html= '<div id="ilikewrap"><div id="ilikeheader">';
		$html.= '<div class="like_right'.$this->params->get('theme_boxes').'"><div id="iliketitle">'.$this->params->get('desc_before_button').'</div>';
        if ($user->id == 0) {
            $html.='<a href="'.JRoute::_('index.php?option=com_users&layout=login').'" class="thumb up_trigger"><span class="icon">'.$this->params->get('like_button_label').'</span></a>';
            if($this->params->get('enable_dislike',1)){
                $html.='<a href="'.JRoute::_('index.php?option=com_users&layout=login').'"  class="thumb down_trigger " ><span class="icon">&nbsp;</span></a>';
            }
        } else if (!$currentlike) {
            $html.='<a data-id="' . $id . '" data-option=' . $option . ' onclick="return false;" class="thumb up_trigger xplike"><span class="icon">'.$this->params->get('like_button_label').'</span></a>';
            if($this->params->get('enable_dislike',1)){
                $html.='<a data-id="' . $id . '" data-option=' . $option . ' onclick="return false;"  class="thumb down_trigger xpdislike" ><span class="icon">&nbsp;</span></a>';
            }
        } else if ($useraction) {
            $html.='<a data-id="' . $id . '" data-option=' . $option . ' onclick="return false;" class="thumb up_trigger ' . ($useraction->value ? 'up_activer' : '') . '"><span class="icon">'.$this->params->get('like_button_label').'</span></a>';
            if($this->params->get('enable_dislike',1)){  
                $html.='<a data-id="' . $id . '" data-option=' . $option . ' onclick="return false;"  class="thumb down_trigger ' . ($useraction->value ? '' : 'down_activer') . '" ><span class="icon">&nbsp;</span></a>';
            }
        } else {
            $html.='<a data-id="' . $id . '" data-option=' . $option . ' onclick="return false;" class="thumb up_trigger xplike"><span class="icon">'.$this->params->get('like_button_label').'</span></a>';
            if($this->params->get('enable_dislike',1)){  
                $html.='<a data-id="' . $id . '" data-option=' . $option . ' onclick="return false;"  class="thumb down_trigger xpdislike" ><span class="icon">&nbsp;</span></a>';
            }
        };
        if ($currentlike) {
            $total = $currentlike->likes + $currentlike->dislikes;
			if (!$total) {$plike=0;$pdislike=0;}
			else
			{
            $plike = ($currentlike->likes / $total) * 100;
            $pdislike = ($currentlike->dislikes / $total) * 100;
			}
            $html.='<div class="xplikebar"><div style="width:' . $plike . '%" class="xplikebar-like"></div><div style="width:' . $pdislike . '%" class="xplikebar-dislike"></div></div>';
            $html.='<div class="" id="thumbs_count_feedback"><strong class="thumbs_up">' . $currentlike->likes . '</strong> '.JText::_("PLG_XP_LIKE").', <strong class="thumbs_down">' . $currentlike->dislikes . '</strong> '.JText::_("PLG_XP_DISLIKE").'';
        } else {
            $html.='<div class="xplikebar"><div style="width:0%" class="xplikebar-like"></div><div style="width:0%" class="xplikebar-dislike"></div></div>';
            $html.='<div class="" id="thumbs_count_feedback"><strong class="thumbs_up">0</strong> '.JText::_("PLG_XP_LIKE").',<strong class="thumbs_down">0</strong> '.JText::_("PLG_XP_DISLIKE").'';
        }
        $html.='</div></div></div></div>'; 
        }
        else {
            $html= '<div id="ilikewrap"><div id="ilikeheader">';
            $html.= '<div class="like_right'.$this->params->get('theme_boxes').'"><div id="iliketitle">'.$this->params->get('desc_before_button').'</div>';
            if (!$currentlike) {
                $html.='<a data-id="' . $id . '" data-option=' . $option . ' onclick="return false;" class="thumb up_trigger xplike"><span class="icon">'.$this->params->get('like_button_label').'</span></a>';
                if($this->params->get('enable_dislike',1)){
                    $html.='<a data-id="' . $id . '" data-option=' . $option . ' onclick="return false;"  class="thumb down_trigger xpdislike" ><span class="icon">&nbsp;</span></a>';
                }
            } else if ($useraction) {
                $html.='<a data-id="' . $id . '" data-option=' . $option . ' onclick="return false;" class="thumb up_trigger ' . ($useraction->value ? 'up_activer' : '') . '"><span class="icon">'.$this->params->get('like_button_label').'</span></a>';
                if($this->params->get('enable_dislike',1)){  
                    $html.='<a data-id="' . $id . '" data-option=' . $option . ' onclick="return false;"  class="thumb down_trigger ' . ($useraction->value ? '' : 'down_activer') . '" ><span class="icon">&nbsp;</span></a>';
                }
            } else {
                $html.='<a data-id="' . $id . '" data-option=' . $option . ' onclick="return false;" class="thumb up_trigger xplike"><span class="icon">'.$this->params->get('like_button_label').'</span></a>';
                if($this->params->get('enable_dislike',1)){  
                    $html.='<a data-id="' . $id . '" data-option=' . $option . ' onclick="return false;"  class="thumb down_trigger xpdislike" ><span class="icon">&nbsp;</span></a>';
                }
            };
            if ($currentlike) {
                $total = $currentlike->likes + $currentlike->dislikes;
                if (!$total) {$plike=0;$pdislike=0;}
                else
                {
                $plike = ($currentlike->likes / $total) * 100;
                $pdislike = ($currentlike->dislikes / $total) * 100;
                }
                $html.='<div class="xplikebar"><div style="width:' . $plike . '%" class="xplikebar-like"></div><div style="width:' . $pdislike . '%" class="xplikebar-dislike"></div></div>';
                $html.='<div class="" id="thumbs_count_feedback"><strong class="thumbs_up">' . $currentlike->likes . '</strong> '.JText::_("PLG_XP_LIKE").', <strong class="thumbs_down">' . $currentlike->dislikes . '</strong> '.JText::_("PLG_XP_DISLIKE").'';
            } else {
                $html.='<div class="xplikebar"><div style="width:0%" class="xplikebar-like"></div><div style="width:0%" class="xplikebar-dislike"></div></div>';
                $html.='<div class="" id="thumbs_count_feedback"><strong class="thumbs_up">0</strong> '.JText::_("PLG_XP_LIKE").',<strong class="thumbs_down">0</strong> '.JText::_("PLG_XP_DISLIKE").'';
            }
            $html.='</div></div></div></div>';              
        }   
        return $html;
    }

}
